import React from "react";
import {message, Tooltip} from "antd";
import copy from "copy-to-clipboard";
import "./index.css";

interface TitleSpanProps {
  className?: string;
  children: any;
  style?: any;
  noTitle?: boolean;
  overlayClassName?: string;
  onDoubleClick?: () => void;
}

const TitleSpan: React.FC<TitleSpanProps> = props => {

  const extraProps: any = {
    className: "smTitleSpan",
    onDoubleClick: props.onDoubleClick
  };

  if (!extraProps.onDoubleClick) {
    extraProps.onDoubleClick = () => {
      if (copy(props.children)) {
        message.success("复制成功");
      }
    };
  }

  if (props.style) {
    extraProps.style = props.style;
  }

  if (props.className) {
    extraProps.className += " " + props.className;
  }

  if (props.noTitle) {
    return <span {...extraProps}>{props.children}</span>;
  }

  return (
    <Tooltip
      overlayClassName={props.overlayClassName}
      title={props.children}
      placement="topLeft"
    >
      <span {...extraProps}>{props.children}</span>
    </Tooltip>
  );
};

export default TitleSpan;
